//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pt;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicSalesCodeDao;
import com.aidong.dao.wills.PtCoachCodeDao;
import com.aidong.dao.wills.PtContractDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.DicSalesCode;
import com.aidong.model.wills.PtCoachCode;
import com.aidong.util.ExportXlsx;
import com.aidong.web.controller.BaseController;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"ptContractQuery"})
public class PtContractQueryController extends BaseController {
    @Resource
    PtContractDao ptContractDao;
    @Resource
    PtCoachCodeDao ptCoachCodeDao;
    @Resource
    DicSalesCodeDao dicSalesCodeDao;

    public PtContractQueryController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return (new ModelAndView("pt/contractquery/list")).addObject("shopName", this.getTypeList("shopName")).addObject("saleType", this.getTypeList("ptSaleType")).addObject("itemType", this.getTypeList("ptItemType")).addObject("dt1", sdf.format(new Date(System.currentTimeMillis() - 86400000L))).addObject("dt2", sdf.format(new Date()));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "saleType",required = false) String saleType, @RequestParam(value = "itemType",required = false) String itemType, @RequestParam(value = "contractCode",required = false) String contractCode, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "cardCode",required = false) String cardCode, @RequestParam(value = "coachCode",required = false) String coachCode, @RequestParam(value = "salesCode",required = false) String salesCode, @RequestParam(value = "date",required = false) String date, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "c.shopCode", shopCode);
        cd.eq("c.saleType", saleType);
        cd.eq("c.contractCode", contractCode);
        cd.eq("c.memberCode", memberCode);
        cd.eq("m.cardCode", cardCode);
        cd.eq("c.coachCode", coachCode);
        cd.eq("i.itemType", itemType);
        ConditionMap mv;
        if(!this.chkNull(salesCode)) {
            mv = new ConditionMap(1);
            mv.eq("c.salesCode", salesCode);
            mv.eq("c.salesCode2", salesCode);
            cd.addSub(mv);
        }

        if(this.queryOwn()) {
            mv = new ConditionMap(1);
            mv.eq("c.coachCode", this.getLoginUserCode());
            mv.eq("c.salesCode", this.getLoginUserCode());
            mv.eq("c.salesCode2", this.getLoginUserCode());
            mv.eq("c.createUser", this.getLoginUserCode());
            cd.addSub(mv);
        }

        if(!this.chkNull(date) && date.length() > 7) {
            Date mv1 = this.getDateFrom(date.substring(0, 7) + "-01");
            if(mv1 != null) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(mv1);
                cal.add(2, 1);
                this.ptContractDao.queryPageList(pg, cd, mv1, cal.getTime());
                Iterator i$ = pg.getResultList().iterator();

                while(i$.hasNext()) {
                    ContractInfo contract = (ContractInfo)i$.next();
                    contract.setStatus(Integer.valueOf(contract.getEndTime().getTime() > System.currentTimeMillis()?1:2));
                }
            }
        }

        ModelAndView mv2 = new ModelAndView("pt/contractquery/_list");
        mv2.addObject("list", pg.getResultList());
        mv2.addObject("pg", pg);
        return mv2;
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "saleType",required = false) String saleType, @RequestParam(value = "itemType",required = false) String itemType, @RequestParam(value = "contractCode",required = false) String contractCode, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "cardCode",required = false) String cardCode, @RequestParam(value = "coachCode",required = false) String coachCode, @RequestParam(value = "salesCode",required = false) String salesCode, @RequestParam(value = "date",required = false) String date, HttpServletResponse response) {
        try {
            Pagination e = new Pagination(1, 600000);
            e.setPage(Integer.valueOf(1), Integer.valueOf(1));
            ConditionMap cd = new ConditionMap();
            this.addConditionShopCode(cd, "c.shopCode", shopCode);
            cd.eq("c.saleType", saleType);
            cd.eq("c.contractCode", contractCode);
            cd.eq("c.memberCode", memberCode);
            cd.eq("m.cardCode", cardCode);
            cd.eq("c.coachCode", coachCode);
            cd.eq("i.itemType", itemType);
            ConditionMap dtLast;
            if(!this.chkNull(salesCode)) {
                dtLast = new ConditionMap(1);
                dtLast.eq("c.salesCode", salesCode);
                dtLast.eq("c.salesCode2", salesCode);
                cd.addSub(dtLast);
            }

            if(this.queryOwn()) {
                dtLast = new ConditionMap(1);
                dtLast.eq("c.coachCode", this.getLoginUserCode());
                dtLast.eq("c.salesCode", this.getLoginUserCode());
                dtLast.eq("c.salesCode2", this.getLoginUserCode());
                dtLast.eq("c.createUser", this.getLoginUserCode());
                cd.addSub(dtLast);
            }

            Date dtLast1 = new Date();
            if(!this.chkNull(date) && date.length() > 7) {
                Date xls = this.getDateFrom(date.substring(0, 7) + "-01");
                if(xls != null) {
                    Calendar sdf = Calendar.getInstance();
                    sdf.setTime(xls);
                    sdf.add(2, 1);
                    dtLast1 = sdf.getTime();
                    this.ptContractDao.queryPageList(e, cd, xls, sdf.getTime());
                    Iterator sdf2 = e.getResultList().iterator();

                    while(sdf2.hasNext()) {
                        ContractInfo i$ = (ContractInfo)sdf2.next();
                        i$.setStatus(Integer.valueOf(i$.getEndTime().getTime() > System.currentTimeMillis()?1:2));
                    }
                }
            }

            ExportXlsx xls1 = new ExportXlsx(response, "私教课时剩余表", "私教课时剩余表");
            xls1.addHead(new String[]{"门店编号", "门店名称", "会员号", "会员姓名", "会员卡号", "课程编号", "课程名称", "教练编号", "教练姓名", "合同号", "销售类型", "购买日期", "截止日期", "购买金额", "购买课时", "上月结余课时", "本月新购课时", "本月消耗课时", "转出课时", "本月剩余课时", "课时单价", "合同状态", "上次上课时间"});
            xls1.setWidth(new int[]{10, 10, 15, 10, 10, 10, 20, 10, 10, 12, 10, 12, 12, 10, 10, 10, 10, 10, 10, 10, 10, 10, 12});
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(e.getResultList() != null) {
                Iterator i$1 = e.getResultList().iterator();

                while(i$1.hasNext()) {
                    ContractInfo contract = (ContractInfo)i$1.next();
                    xls1.addRow(new Object[]{contract.getShopCode(), this.cache.getValue("shopName", contract.getShopCode()), contract.getMemberCode(), contract.getMemberName(), contract.getCardCode(), contract.getPtCode(), this.cache.getValue("ptItemName", contract.getPtCode()), contract.getCoachCode(), this.cache.getValue("ptCoachName", contract.getCoachCode()), contract.getContractCode(), this.cache.getValue("ptSaleType", contract.getSaleType()), sdf1.format(contract.getSaleDate()), sdf1.format(contract.getEndTime()), contract.getFactPrice(), contract.getQuantity(), Integer.valueOf(contract.getQuantity().intValue() - contract.getQuantity3().intValue() - contract.getQuantity1().intValue()), contract.getQuantity3(), contract.getQuantity2(), contract.getQuantity4(), Integer.valueOf(contract.getQuantity().intValue() - contract.getQuantity1().intValue() - contract.getQuantity2().intValue() - contract.getQuantity4().intValue()), contract.getQuantity().intValue() == 0?null:Double.valueOf(contract.getFactPrice().doubleValue() / (double)contract.getQuantity().intValue()), !this.chkNull(contract.getOutType()) && contract.getOutTime() != null && contract.getOutTime().getTime() < dtLast1.getTime()?this.cache.getValue("ptOutType", contract.getOutType()):(contract.getEndTime().getTime() > dtLast1.getTime()?"正常":"已过期"), contract.getClassTime() == null?null:sdf1.format(contract.getClassTime())});
                }
            }

            xls1.close();
            this.addUserLog("导出私教课时剩余表", e.getResultList().size() + "条记录");
        } catch (Exception var19) {
            this.log.error("import", var19);
        }

        return null;
    }

    @RequestMapping({"getSales"})
    @ResponseBody
    public String getSales(@RequestParam("code") String code) {
        DicSalesCode sales = this.dicSalesCodeDao.getByCode(code);
        if(sales == null) {
            sales = this.dicSalesCodeDao.getByName(code);
        }

        if(sales == null) {
            return this.getResult(false, "销售员编号错误");
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append("{code:\"").append(sales.getCode()).append("\"").append(",name:\"").append(sales.getChName()).append("\"").append("}");
            return this.getSuccess(sb.toString(), "");
        }
    }

    @RequestMapping({"getCoach"})
    @ResponseBody
    public String getCoach(@RequestParam("code") String code) {
        PtCoachCode coach = this.ptCoachCodeDao.getByCode(code);
        if(coach == null) {
            coach = this.ptCoachCodeDao.getByName(code);
        }

        if(coach == null) {
            return this.getResult(false, "教练编号错误");
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append("{code:\"").append(coach.getCode()).append("\"").append(",name:\"").append(this.convStr(coach.getChName())).append("\"").append("}");
            return this.getSuccess(sb.toString(), "");
        }
    }
}
